package com.example.indenture.company.service.impl;


import com.example.indenture.common.Constant;
import com.example.indenture.common.vo.Result;
import com.example.indenture.common.vo.Search;
import com.example.indenture.company.dao.CompanyDao;
import com.example.indenture.company.entity.Company;
import com.example.indenture.company.service.CompanyService;
import com.example.indenture.race.entity.Race;
import com.example.indenture.util.PasswordUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpSession;
import javax.transaction.Transactional;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.Optional;

@Service
public class CompanyServiceImpl implements CompanyService {

    @Autowired
    private CompanyDao companyDao;

    @Override
    @Transactional
    public Result<Company> insertCompany(Company company,HttpSession httpSession) {
        Company temp1 = companyDao.selectCompanyByName(company.getCompanyName());
        if (temp1 != null) {
            return Result.failed("用户名重复。");
        }
        //初始化company一些属性
        company.setCreateDate(LocalDateTime.now());
        company.setUpdateTime(LocalDateTime.now());
        company.setPassword(PasswordUtil.encrypt(company.getPassword()));
        //添加company
        companyDao.insertCompany(company);
        //存储Session
        httpSession.setAttribute(Constant.SESSION_USERINFO_KEY, company);
        //返回结果集
        return Result.ok(company);
    }

    @Override
    @Transactional
    public Result<Company> updateCompany(Company company) {
        Company temp1 = companyDao.selectCompanyByName(company.getCompanyName());
        if (temp1 != null) {
            return Result.failed("用户名重复。");
        }
        //初始化属性
        company.setUpdateTime(LocalDateTime.now());
        company.setPassword(PasswordUtil.encrypt(company.getPassword()));
        //更新
        companyDao.updateCompany(company);
        //返回结果集
        return Result.ok(company);
    }

    @Override
    @Transactional
    public Result<Company> loginCompany(String username, String password, HttpSession httpSession) {
        if (!StringUtils.hasLength(username) || !StringUtils.hasLength(password)) {
            return Result.failed("用户名或密码为空!");
        }
//        password = PasswordUtil.encrypt(password);
        final Company company = companyDao.getPasswordByName(username);
        if (PasswordUtil.decrypt(password, company.getPassword())) {
            httpSession.setAttribute(Constant.SESSION_USERINFO_KEY, company);
            company.setPassword("");
            return Result.ok(company);
        } else {
            return Result.failed("验证失败!");
        }
    }

    @Override
    @Transactional
    public PageInfo<Company> getCompaniesBySearch(Search search) {
        search.initSearch();
        PageHelper.startPage(search.getCurrentPage(), search.getPageSize());
        return new PageInfo<>(Optional
                .ofNullable(companyDao.getCompanyBySearch(search))
                .orElse(Collections.emptyList()));
    }

    @Override
    public Result<Object> updateCompanyUrl(int id, String url) {
        companyDao.updateCompanyUrl(id, url);
        return Result.ok();
    }

    @Override
    public Company getCompanyById(Integer id) {
        return companyDao.selectCompanyById(id);
    }


}
